﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet;
using BaseSet.Entity.Common.Controls;
using BaseSet.Entity;
using BaseSet.Data;

namespace WebSite
{
    public class PreBasePage:Page
    {

        //过滤视图状态中的乱码数据
        protected LosFormatter losFormatter = new LosFormatter();
        protected override object LoadPageStateFromPersistenceMedium()
        {
            string key = Request.RawUrl + "__viewstate";
            if (Session[key] != null)
            {
                System.IO.MemoryStream stream = (System.IO.MemoryStream)Session[key];
                stream.Seek(0, System.IO.SeekOrigin.Begin);
                return losFormatter.Deserialize(stream);
            }
            return null;
        }
        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
            string key = Request.RawUrl + "__viewstate";
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            losFormatter.Serialize(stream, viewState);
            stream.Flush();
            Session[key] = stream;
        }

        /// <summary>
        /// 当前页面的url
        /// </summary>
        public string CurrentPageUrl
        {
            get
            {
                string relUrl = "";
                string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri.Replace(UserBase.GetServerPath(), "").Trim();

                if (url.IndexOf('?') > 0)
                {
                    relUrl = url.Substring(0, url.IndexOf('?'));
                }
                else
                {
                    relUrl = url;
                }

                return relUrl;
            }
        }

        /// <summary>
        /// 获取url传递过来的ID，主要用于url重写时确定目标对象
        /// </summary>
        public string ObjectID
        {
            get
            {
                string id = System.Web.HttpContext.Current.Request["ID"];

                return id;
            }
        }


        protected void PagerInit(URLPager uPager)
        {
            Session["selectPageCount"] = null;
            uPager.PageIndex = 0;
        }

        /// <summary>
        /// 生成翻页设置控件
        /// </summary>
        /// <returns></returns>
        protected string BuildPageLinks()
        {
            string rawUrl = Request.RawUrl;
            rawUrl = rawUrl.Replace("{", "{{").Replace("}", "}}");
            string links = " 每页显示条数：" + "<li><a href='" + BaseSetHelper.AddParamToUrl(rawUrl, "pageCount", "3") + "'{0} >3</a> </li>" + ",&nbsp;" +
            "<li><a href='" + BaseSetHelper.AddParamToUrl(rawUrl, "pageCount", "25") + "'{1}>25</a></li>" + ",&nbsp;" +
            "<li><a href='" + BaseSetHelper.AddParamToUrl(rawUrl, "pageCount", "50") + "'{2}>50</a></li>";

            string css99, css0, css1;
            css99 = css0 = css1 = "";
            if (PageCount == 3) css99 = " class=\"current\"";
            if (PageCount == 25) css0 = " class=\"current\"";
            if (PageCount == 50) css1 = " class=\"current\"";
            links = string.Format(links, css99,
                css0, css1);
            return links.Replace("{{", "{").Replace("}}", "}");
        }

        /// <summary>
        /// 设置翻页控件属性
        /// </summary>
        /// <param name="MemberUPager"></param>
        protected void PageConfig(URLPager MemberUPager)
        {
            if (Request["pageCount"] != null && Request["pageCount"].ToString() != "")
            {
                string selectPage = Request["pageCount"].ToString();
                int pageCount = Int32.Parse(selectPage);
                MemberUPager.PageSize = pageCount;
                if (Session["selectPageCount"] == null || Session["selectPageCount"].ToString() != selectPage)
                {
                    MemberUPager.PageIndex = 1;
                }
                else
                {
                    MemberUPager.PageIndex = PageNumber;
                }
                Session["selectPageCount"] = selectPage;
            }
            else
            {
                MemberUPager.PageIndex = PageNumber;
            }

            return;
        }

        /// <summary>
        /// 获取PrefixText
        /// </summary>
        /// <param name="MemberUPager"></param>
        /// <returns></returns>
        protected string GetPreFixText(URLPager MemberUPager)
        {
            string prefix = "共" + MemberUPager.ItemCount.ToString() + "条记录 ·  第" + MemberUPager.PageIndex + "/" + MemberUPager.MaxPages + "  页 · ";
            return prefix;
        }

        private int _resultsPageNumber = 1;

        /// <summary>
        /// 当前页
        /// </summary>
        protected int PageNumber
        {
            get
            {
                if (Request.QueryString[Keys.QRYSTR_PAGEINDEX] != null)
                    _resultsPageNumber = Convert.ToInt32(Request.QueryString[Keys.QRYSTR_PAGEINDEX]);
                return _resultsPageNumber;
            }
        }

        /// <summary>
        /// url格式设置
        /// </summary>
        protected string UrlFormat
        {
            get
            {
                return BaseSetHelper.AddParamToUrl(Request.RawUrl.Replace("{", "{{").Replace("}", "}}"), Keys.QRYSTR_PAGEINDEX, "{0}");
            }
        }

        /// <summary>
        /// 页数
        /// </summary>
        protected int PageCount
        {
            get
            {
                int count = 0;
                if (Request["pageCount"] != null && Request["pageCount"].ToString() != "")
                {
                    string selectPage = Request["pageCount"].ToString();
                    count = Int32.Parse(selectPage);
                }
                return count;
            }
        }
       
    }
}